package com.seven.restaurant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seven.restaurant.entity.po.FoodPO;
import com.seven.restaurant.entity.po.UserPO;
import com.seven.restaurant.entity.request.LoginRequest;
import com.seven.restaurant.entity.vo.OrderDetailsVO;
import com.seven.restaurant.entity.vo.UserDetailsVO;
import com.seven.restaurant.exception.GlobalException;
import com.seven.restaurant.exception.InfoCode;
import com.seven.restaurant.mapper.UserMapper;
import com.seven.restaurant.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author BaiBan
 * @since 2022-07-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements UserService {

    @Autowired
    UserMapper mapper;

    @Override
    public Long login(LoginRequest loginRequest) throws GlobalException {
        // 效验用户名 密码 校验码合法

        // 获取用户 ? 抛出用户不存在异常
        QueryWrapper<UserPO> qw = new QueryWrapper<>();
        qw.eq("username", loginRequest.getUsername());
        UserPO user = mapper.selectOne(qw);
        if (user == null)
            throw new GlobalException(InfoCode.USERNAME_FAIL);
        // 获取密码
        String password = user.getPassword();
        // 加盐匹配? 返回凭证: 抛出密码错误异常
        if (password.equals(loginRequest.getPassword())) {
            return user.getId();
        }
        else throw new GlobalException(InfoCode.LOGIN_FAIL);
    }

    @Override
    public Long register(UserPO user) throws GlobalException {
        // 效验用户名 密码 类型 头像合法

        // 用户名被注册? 注册成功: 注册失败
        QueryWrapper<UserPO> qw = new QueryWrapper<>();
        qw.eq("username", user.getUsername());
        if (mapper.selectOne(qw) != null)
            throw new GlobalException(InfoCode.USERNAME_EXISTS_ERROR);

        mapper.insert(user);
        return user.getId();
    }

    @Override
    public UserDetailsVO details(Integer id) throws GlobalException {
        UserPO user = mapper.selectById(id);
        if(user == null)
            throw new GlobalException(InfoCode.USERNAME_FAIL);
        return toUserDetailsVO(user);
    }

    @Override
    public IPage<UserDetailsVO> list(Integer current) {

        Page<OrderDetailsVO> page = new Page<>(current, 10);
        QueryWrapper<FoodPO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id")  // 按照主键
                .eq("del", 0);
        return mapper.selectDetailsPage(page, wrapper);
    }

    @Override
    public boolean delete(Integer id) throws GlobalException {
        UserPO user = mapper.selectById(id);
        // 用户不存在或已删除
        if(user == null || user.getDel() == 1)
            throw new GlobalException(InfoCode.USERNAME_FAIL);
        user.setDel(1);
        mapper.updateById(user);
        return true;
    }

    @Override
    public boolean update(UserDetailsVO user) {
        return false;
    }

    private final String icon = "http://114.116.112.52:8080/data/header/";
    /**
     * UserPO 转 UserVO
     */
    private UserDetailsVO toUserDetailsVO(UserPO user) {
        UserDetailsVO vo = new UserDetailsVO();
        vo.setId(user.getId());
        vo.setType(user.getType());
        vo.setUsername(user.getUsername());
        vo.setIcon(icon + user.getIcon());
        return vo;
    }
}